package org.sam.swing.table.editor;

import java.awt.Component;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.Date;
import java.util.EventObject;
import java.util.Objects;

import javax.swing.AbstractCellEditor;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.table.TableCellEditor;
import javax.swing.tree.TreeCellEditor;

import org.sam.swing.JSDateSpinner;

/**
 * 日期的编辑框
 * 
 * 
 * @author sam
 *
 */
public class JSTableLocalTimeSpinnerEditor extends AbstractCellEditor implements TreeCellEditor, TableCellEditor {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2254897074135560584L;

	/**
	 * 当前的掩码格式控件
	 */
	private JSDateSpinner editor;
	
	/**
	 * 当前缓存的时间
	 */
	private Date current = Calendar.getInstance().getTime();

	/**
	 * 当前的掩码格式控件
	 * 
	 * @return
	 */
	public JSDateSpinner getEditor() {
		return editor;
	}

	/**
	 * 当前的掩码格式控件
	 * 
	 * @param editor
	 */
	public void setEditor(JSDateSpinner editor) {
		this.editor = editor;
	}

	/**
	 * 几次鼠标操作开始编辑
	 */
	private int clickCountToStart = 2;

	/**
	 * 几次鼠标操作开始编辑
	 * 
	 * @return
	 */
	public int getClickCountToStart() {
		return clickCountToStart;
	}

	/**
	 * 几次鼠标操作开始编辑
	 * 
	 * @param clickCountToStart
	 */
	public void setClickCountToStart(int clickCountToStart) {
		this.clickCountToStart = clickCountToStart;
	}

	/**
	 * 不带掩码格式的构造函数
	 */
	public JSTableLocalTimeSpinnerEditor() {
		super();
		editor = new JSDateSpinner();
	}

	/**
	 * 带有掩码格式的构造函数
	 * 
	 * @param format
	 */
	public JSTableLocalTimeSpinnerEditor(String format) {
		super();
		editor = new JSDateSpinner(format);
	}

	/**
	 * create with SimpleDateFormat
	 * 
	 * @param format
	 */
	public JSTableLocalTimeSpinnerEditor(SimpleDateFormat format) {
		super();
		editor = new JSDateSpinner(format);
	}

	/**
	 * 带有控件的构造函数
	 * 
	 * @param editor
	 */
	public JSTableLocalTimeSpinnerEditor(JSDateSpinner editor) {
		super();
		this.setEditor(editor);
	}

	@SuppressWarnings("deprecation")
	@Override
	public Object getCellEditorValue() {
		try {

			if (JSDateSpinner.isNull((Date) editor.getValue())) {
				return 0;
			} else {
				Date date = (Date)editor.getValue();
				int secondes = date.getHours() * 3600 + date.getMinutes() * 60 + date.getSeconds();
				return secondes;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@SuppressWarnings("deprecation")
	@Override
	public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
		if (Objects.isNull(value)){
			value = 0;
		}
		if (value instanceof Integer){
			LocalTime time = LocalTime.ofSecondOfDay((Integer)value);
			current.setHours(time.getHour());
			current.setMinutes(time.getMinute());
			current.setSeconds(time.getSecond());
			this.editor.setValue(current);
		} else if(value instanceof Long){
			LocalTime time = LocalTime.ofSecondOfDay((Long)value);
			current.setHours(time.getHour());
			current.setMinutes(time.getMinute());
			current.setSeconds(time.getSecond());
			this.editor.setValue(current);
		} else {
			this.editor.setValue(value);
		}
		
		return this.editor;
	}

	@SuppressWarnings("deprecation")
	@Override
	public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded,
			boolean leaf, int row) {

		if (Objects.isNull(value)){
			value = 0;
		}
		if (value instanceof Integer){
			LocalTime time = LocalTime.ofSecondOfDay((Integer)value);
			current.setHours(time.getHour());
			current.setMinutes(time.getMinute());
			current.setSeconds(time.getSecond());
			this.editor.setValue(current);
		} else if(value instanceof Long){
			LocalTime time = LocalTime.ofSecondOfDay((Long)value);
			current.setHours(time.getHour());
			current.setMinutes(time.getMinute());
			current.setSeconds(time.getSecond());
			this.editor.setValue(current);
		} else {
			this.editor.setValue(value);
		}
		
		return this.editor;
	}

	/**
	 * 什么情况下开始允许编辑
	 */
	@Override
	public boolean isCellEditable(EventObject anEvent) {
		if (anEvent instanceof MouseEvent) {
			return ((MouseEvent) anEvent).getClickCount() >= clickCountToStart;
		}
		return true;
	}
}
